package com.atwisdom.star.core.dao.pojo.functionClass.statistics;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.atwisdom.star.core.dao.pojo.modelClass.BaseModel;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.baomidou.mybatisplus.extension.handlers.FastjsonTypeHandler;
import lombok.Data;

import java.math.BigDecimal;
import java.util.Set;

/**
 * @TableName statistics_model
 */
@TableName(value = "statistics_model", autoResultMap = true)
@Data
public class StatisticsModel extends BaseModel {

    /**
     * 属性信息
     */
    @TableField(value = "hierProperties", typeHandler = FastjsonTypeHandler.class)
    protected JSONObject hierProperties;

    @TableField(exist = false)
    private static final long serialVersionUID = 1L;


    public StatisticsModel() {
    }

    public StatisticsModel(String type, String className) {
        this.type= type;
        this.className = className;
        this.version = new BigDecimal(1.0);
        this.published = true;
        this.setHierProperties(new JSONObject());
        this.setStatusProperties(new JSONArray());
        this.setProperties(new JSONArray());
        this.setWeb(new JSONObject());
        this.setOthers(new JSONObject());
    }

    /**
     * 有参构造方法
     *
     * @param jsonObject 参数
     */
    public StatisticsModel(JSONObject jsonObject) {
        Set<String> keys = jsonObject.keySet();
        this.id = keys.contains("id") ? jsonObject.getLong("id") : null;
        this.type = keys.contains("type") ? jsonObject.getString("type") : "general";
        this.className = jsonObject.getString("className");
        this.version = keys.contains("version") ? jsonObject.getBigDecimal("version") : BigDecimal.valueOf(1.0);
        this.published = keys.contains("published") ? jsonObject.getBoolean("published") : true;
        this.deleteFlag = keys.contains("deleteFlag") ? jsonObject.getBoolean("deleteFlag") : false;
        this.comments = keys.contains("comments") ? jsonObject.getString("comments") : null;
        this.web = keys.contains("web") ? jsonObject.getJSONObject("web") : null;
        this.others = keys.contains("others") ? jsonObject.getJSONObject("others") : null;
        JSONArray properties = keys.contains("properties") ? jsonObject.getJSONArray("properties") : null;
        this.setProperties(properties);
        JSONObject hierProperties = keys.contains("hierProperties") ? jsonObject.getJSONObject("hierProperties") : null;
        this.setHierProperties(hierProperties);
        JSONArray statusProperties = keys.contains("statusProperties") ? jsonObject.getJSONArray("statusProperties") : null;
        this.setStatusProperties(statusProperties);
    }

    public JSONObject fetchJsonObject() {
        JSONObject JsonObject = new JSONObject();
        JsonObject.put("id", this.getId());
        JsonObject.put("type", this.getType());
        JsonObject.put("className", this.getClassName());
        JsonObject.put("version", this.getVersion());
        JsonObject.put("published", this.getPublished());
        JsonObject.put("web", this.getWeb());
        JsonObject.put("others", this.getOthers());
        JsonObject.put("comments", this.getComments());
        JsonObject.put("deleteFlag", this.getDeleteFlag());
        JsonObject.put("createdBy", this.getCreatedBy());
        JsonObject.put("createTime", this.getCreateTime());
        JsonObject.put("updatedBy", this.getUpdatedBy());
        JsonObject.put("updateTime", this.getUpdateTime());
        JsonObject.put("properties", this.getProperties());
        JsonObject.put("hierProperties", this.getHierProperties());
        JsonObject.put("statusProperties", this.getStatusProperties());
        return JsonObject;
    }
}
